package com.lixunhuan.handacount.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lixunhuan.handacount.common.ResultInfo;
import com.lixunhuan.handacount.db.bean.Diary;
import com.lixunhuan.handacount.db.bean.HandAcount;
import com.lixunhuan.handacount.db.bean.User;
import com.lixunhuan.handacount.service.IDiaryService;
import com.lixunhuan.handacount.service.IHandAcountService;
import com.lixunhuan.handacount.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;

/**
 * <p>
 * 用户 前端控制器
 * </p>
 *
 * @author lixunhuan
 * @since 2021-03-19
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;
    @Autowired
    private IDiaryService diaryService;
    @Autowired
    private IHandAcountService handAcountService;
    private static String AUTH_TOKEN = "Authorization";
    @RequestMapping("login")
    public ResultInfo login(@NotNull @RequestParam("uid") String uid,
                            @NotNull @RequestParam("password") String password){
        if (StringUtils.isEmpty(uid)||StringUtils.isEmpty(password)){
            return new ResultInfo(ResultInfo.Status.LOGIN_FAIL);
        }
        if (userService.count(new QueryWrapper<User>().eq("username",uid))<1){
            return new ResultInfo(ResultInfo.Status.FAILURE).message("无该用户,联系管理员添加");
        }
        if (userService.getOne(new QueryWrapper<User>().eq("username",uid)).getPassword().equals(password)) {
            return new ResultInfo(ResultInfo.Status.SUCCESS).result(userService.getOne(new QueryWrapper<User>().eq("username",uid)).getUsername());
        }
        return new ResultInfo(ResultInfo.Status.LOGIN_FAIL);
    }

    @RequestMapping("register")
    public ResultInfo register(@NotNull @RequestParam("uid") String uid,
                               @NotNull @RequestParam("password") String password){
        if (StringUtils.isEmpty(uid)||StringUtils.isEmpty(password)){
            return new ResultInfo(ResultInfo.Status.ERROR_400);
        }
        if (userService.count(new QueryWrapper<User>().eq("username",uid))>0)
            return new ResultInfo(ResultInfo.Status.USER_EXISTS);
        User user = new User();

        user.setUsername(uid);
        user.setPassword(password);
        userService.save(user);
        return new ResultInfo(ResultInfo.Status.SUCCESS).message("注册成功");
    }
    @RequestMapping("delete")
    public ResultInfo delete(HttpServletRequest httpServletRequest){
        String uid = httpServletRequest.getHeader(AUTH_TOKEN);
        userService.remove(new QueryWrapper<User>().eq("username",uid));
        //用户注销，对应其的日记也删除
        diaryService.remove(new QueryWrapper<Diary>().eq("uid",uid));
        handAcountService.remove(new QueryWrapper<HandAcount>().eq("uid",uid));
        return new ResultInfo(ResultInfo.Status.SUCCESS).message("注销成功");
    }

}
